package com.smart.translator.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;

public class BlePermissionHelper {
    private static final String TAG = "BlePermissionHelper";
    
    // 权限请求码
    public static final int REQUEST_BLE_PERMISSIONS = 1001;
    
    /**
     * 检查BLE相关权限
     */
    public static boolean checkBlePermissions(Context context) {
        List<String> missingPermissions = getMissingBlePermissions(context);
        return missingPermissions.isEmpty();
    }
    
    /**
     * 获取缺失的BLE权限列表
     */
    public static List<String> getMissingBlePermissions(Context context) {
        List<String> missingPermissions = new ArrayList<>();
        
        // Android 12及以上版本需要的权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.BLUETOOTH_SCAN);
            }
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.BLUETOOTH_CONNECT);
            }
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.BLUETOOTH_ADVERTISE);
            }
        } else {
            // Android 11及以下版本需要的权限
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.BLUETOOTH);
            }
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.BLUETOOTH_ADMIN);
            }
        }
        
        // 位置权限（Android 12以下需要）
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }
        }
        
        // 前台服务权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED) {
            missingPermissions.add(Manifest.permission.FOREGROUND_SERVICE);
        }
        
        // Android 14及以上需要前台服务类型权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.FOREGROUND_SERVICE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(Manifest.permission.FOREGROUND_SERVICE_LOCATION);
            }
        }
        
        return missingPermissions;
    }
    
    /**
     * 请求BLE权限
     */
    public static void requestBlePermissions(Activity activity) {
        List<String> missingPermissions = getMissingBlePermissions(activity);
        
        if (!missingPermissions.isEmpty()) {
            android.util.Log.d(TAG, "请求BLE权限: " + missingPermissions);
            ActivityCompat.requestPermissions(
                activity,
                missingPermissions.toArray(new String[0]),
                REQUEST_BLE_PERMISSIONS
            );
        } else {
            android.util.Log.d(TAG, "所有BLE权限已授予");
        }
    }
    
    /**
     * 处理权限请求结果
     */
    public static boolean handlePermissionResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_BLE_PERMISSIONS) {
            boolean allGranted = true;
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    android.util.Log.e(TAG, "权限被拒绝: " + permissions[i]);
                }
            }
            
            if (allGranted) {
                android.util.Log.d(TAG, "所有BLE权限已授予");
            } else {
                android.util.Log.e(TAG, "部分BLE权限被拒绝");
            }
            
            return allGranted;
        }
        
        return false;
    }
    
    /**
     * 检查是否应该显示权限说明
     */
    public static boolean shouldShowPermissionRationale(Activity activity, String permission) {
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
    }
    
    /**
     * 获取权限说明文本
     */
    public static String getPermissionRationaleText(String permission) {
        switch (permission) {
            case Manifest.permission.BLUETOOTH_SCAN:
                return "需要蓝牙扫描权限来发现BLE设备";
            case Manifest.permission.BLUETOOTH_CONNECT:
                return "需要蓝牙连接权限来连接BLE设备";
            case Manifest.permission.BLUETOOTH_ADVERTISE:
                return "需要蓝牙广播权限来广播BLE服务";
            case Manifest.permission.ACCESS_FINE_LOCATION:
                return "需要精确位置权限来扫描BLE设备";
            case Manifest.permission.ACCESS_COARSE_LOCATION:
                return "需要粗略位置权限来扫描BLE设备";
            case Manifest.permission.FOREGROUND_SERVICE:
                return "需要前台服务权限来保持BLE连接";
            case Manifest.permission.FOREGROUND_SERVICE_LOCATION:
                return "需要位置前台服务权限来保持BLE连接";
            default:
                return "需要此权限来使用BLE功能";
        }
    }
} 